<style>
.il2 { display: inline-block !important; width: 293px; }
.pure_table_extra_ab { width: 293px; border: 1px solid #aaa !important; }
.pure_table_extra_ab td { height: 20px !important; }
.pure_table_extra_ab a { color: #666; }
.pure_table_extra_ab * { font-size: 12px !important; font-family: Trebuchet MS !important; }
.pure_table_extra_ab .date { float: right; color: #666; font-size: 11px !important; }

.pure_form_extra { background: #fff; padding: 10px; border: 1px solid #aaa; border-radius: 3px; }
.pure_form_extra span { font-size: 12px !important; color: #666; font-family: Trebuchet MS; }
.pure_form_extra a { font-size: 13px !important; color: #666; font-family: Trebuchet MS; }
table.list { border-left: 1px solid #aaa; border-top: 1px solid #aaa; font-size: 13px; width: 100%; }
table.list th,table.list td { vertical-align: middle; text-align: left;padding: 0 5px; border-right: 1px solid #aaa; border-bottom: 1px solid #aaa; }
table.list th { height: 28px; background-color: #ddd; font-weight: bold; color: #333; }
table.list td { height: 25px; word-wrap: break-word; overflow: hidden; }
table.list tr.rowodd td { background-color: #eee; }
table.list tr.roweven td { background-color: #fff; }
table.list tr.rowodd:hover td { background-color: #f4f3b2; }
table.list tr.roweven:hover td { background-color: #f4f3b2; }
table.list .date { float: right; color: #666; font-size: 11px !important; }
table.list .col { margin-right: 5px; color: #575757; }
</style>

<?php
$pg = Utils::get_param('pg', 1);
$cat = Utils::get_param('cat', 0);
$search = Utils::get_param('search', '');

$opts = new stdClass();
$opts->cd = 't.title LIKE :v1';
$opts->cdv = array(':v1' => '%' . $search . '%');
if ($cat != 0) {
  $opts->cd .= ' AND t.categories__ LIKE :v2';
  $opts->cdv[':v2'] = '%,' . $cat . ',%';
}
$data = $orm->get('pb_ht_article', 't.date', 'DESC', $pg, 25, $opts);

$categories = $orm->get('pb_ht_category', NULL, NULL, NULL, NULL, NULL)->d;

include dirname(__FILE__) . '/../admin_articles/index.srv.php';

$root = new stdClass();
$root->id = 0;
$root->name = '所有栏目';
$root->parent_id = -1;
Server::__create_root($categories, $root);

$rslt = new stdClass();
$rslt->path = array();
__path($root, $cat, $rslt);
if (count($rslt->path) == 0) {
  return;
}

$rslt->children = array();
__children($root, $cat, $rslt);

function __path($node, $cat, $rslt) {
  if ($node->id == $cat) {
    $obj = clone $node;
    unset($obj->__c);
    array_push($rslt->path, $obj);
    return true;
  }
  if ($node->id != 0 || true) {
    $obj = clone $node;
    unset($obj->__c);
    array_push($rslt->path, $obj);
  }
  foreach ($node->__c as $itm) {
    if (__path($itm, $cat, $rslt) == true) {
      return true;
    }
  }
  if ($node->id != 0 || true) {
    array_pop($rslt->path);
  }
}

function __children($node, $cat, $rslt) {
  if ($node->id == $cat) {
    $rslt->children =  $node->__c;
    return;
  }
  foreach ($node->__c as $itm) {
    if ($itm->id == $cat) {
      $rslt->children = &$itm->__c;
      return;
    }
  }
}
?>

<?php if (isset($rslt->path[1]->ad_top) && $rslt->path[1]->ad_top != '') { ?>
<img src="<?php echo $rslt->path[1]->ad_top; ?>" alt="" style="width: 950px;"></img>
<?php } ?>
<table style="width: 950px; margin-top: 5px;" cellpadding=0 cellspacing=0>
  <tr>
    <td style="width: <?php echo ($cat == 0) ? '100%' : '730px' ?>; vertical-align: top; padding-right: 10px;">
      <div class="pure-form pure_form_extra" style="margin-bottom: 10px;">
        <b>您的位置</b>
        <?php foreach ($rslt->path as $itm) { ?>
          <i class="fa fa-arrow-right"></i>
          <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&cat=<?php echo $itm->id; ?>&search=<?php echo urlencode($search); ?>');"><?php echo $itm->name; ?></a>
        <?php } ?>
      </div>
      
      <?php if (count($rslt->children) > 0) { ?>
      <div class="pure-form pure_form_extra" style="margin-bottom: 10px; padding-top: 20px; padding-bottom: 30px;">
        <b style="font-size: 12px;">子目录</b>
        <?php foreach ($rslt->children as $itm) { ?>
          <a style="margin-right: 5px;" href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&cat=<?php echo $itm->id; ?>&search=<?php echo urlencode($search); ?>');"><?php echo $itm->name; ?></a>
        <?php } ?>
      </div>
      <?php } ?>
      
      <div class="pure-form pure_form_extra" style="width: <?php echo ($cat != 0) ? 700 : 950; ?>px; margin-bottom: 5px;">
        <table class="list" cellpadding=0 cellspacing=0>
          <tbody id="art_body"></tbody>
        </table>
        <div>&nbsp;</div>
        <div id="pagination"></div>
      </div>
    </td>
    <?php if ($cat != 0) { ?>
    <td style="width: 230px; vertical-align: top;">
      <div class="pure-form pure_form_extra" style="width: 200px; margin-bottom: 10px; min-height: 440px; overflow: hidden;">
        <legend style="font-size: 13px; font-weight: bold;">广告栏</legend>
        <?php echo (isset($rslt->path[1]->ad_right) && $rslt->path[1]->ad_right != '') ? $rslt->path[1]->ad_right : ''; ?>
      </div>
    </td>
    <?php } ?>
    <tr><td colspan="2"><?php include PLGN_DIR . basename(dirname(__FILE__)) . '/inc.bottom.php'; ?></td></tr>
  </tr>
</table>
<?php if (isset($rslt->path[1]->ad_bottom) && $rslt->path[1]->ad_bottom != '') { ?>
<img src="<?php echo $rslt->path[1]->ad_bottom; ?>" alt="" style="width: 950px; margin-top: 10px;"></img>
<?php } ?>
<div>&nbsp;</div>
<script type="text/javascript">
  var __cat = '<?php echo $cat; ?>';
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __cat = '<?php echo $cat; ?>';
  
  $(function(){
    $('#pagination').pagination({
      items: __data.t,
      itemsOnPage: __data.s,
      currentPage: __data.p,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
        return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&pg=' + pageNumber + '&cat=' + __cat);
      }
    });
    render();
  });


  function render() {
    $('#art_body').empty();
    for (var idx in __data.d) {
      var itm = __data.d[idx];
      $('#art_body').append(
        '<tr class="' + ((idx % 2 ==0) ? 'roweven' : 'rowodd') + '">' +
          '<td>' +
            '<span class="date">' + itm.date.split(' ')[0] + '</span>' +
            '<span class="col">' + ((__cat != 0) ? itm.column__ : '(' + itm.full_column__ + ')') + '</span>' +
            '<a href="#" onclick="return Url.redirect(__redmd, \'' + __sys + '.php?i=' + __id + '&__file=index_article&id=' + itm.id + '&cat=' + __cat + '\');">' + Utils.get_short(itm.title, 30) + '</a>' +
          '</td>' +
        '</tr>'
      );
    }

    if (__data.length == 0) {
      $('#art_body').append('<tr class="roweven"><td colSpan="7">&nbsp;</td></tr>')
    } 
  };
</script>